package aar.test.tessocr;

import android.os.Bundle;
import android.support.design.widget.TabLayout;
import android.support.v4.app.Fragment;
import android.support.v4.view.ViewPager;
import android.view.Gravity;
import android.view.View;
import android.widget.RelativeLayout;
import android.widget.TextView;

import java.util.ArrayList;
import java.util.List;

import aar.test.tessocr.base.BaseActivity;
import aar.test.tessocr.base.BaseFragmentPagerAdapter;
import aar.test.tessocr.fragment.OrderPickedUpFragment;
import aar.test.tessocr.fragment.OrderTimeOutFragment;
import aar.test.tessocr.fragment.OrderUnDeliveredFragment;
import aar.test.tessocr.fragment.OrderUnPickUpFragment;
import aar.test.tessocr.view.SearchHistoryPopwindow;
import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import clx.tessocr.ocreng.R;

/**
 * Created by sunshine on 2020/7/27
 */
public class OrderListActivity extends BaseActivity implements SearchHistoryPopwindow.OnSearchListener {
    @BindView(R.id.tbl_recruit)
    TabLayout tblOrder;
    @BindView(R.id.vp_content)
    ViewPager viewPager;
    @BindView(R.id.et_fragment_search)
    TextView etFragmentSearch;
    @BindView(R.id.view_line)
    View viewLine;
    private BaseFragmentPagerAdapter mAdapter;
    private Integer[] titlesRes = new Integer[]{R.string.order_uundelivered, R.string.order_undelivered, R.string.order_delivered, R.string.order_timeout};
    private String search = "";
    private String type = "";
    private RelativeLayout ll_cabinet_num;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        type = getIntent().getStringExtra("type");
        setContentView(R.layout.activity_common_tab);
        ButterKnife.bind(this);
        initView();
    }

    private void initView() {
        ll_cabinet_num = findViewById(R.id.ll_cabinet_num);
        ll_cabinet_num.setVisibility(View.GONE);
        List<String> titles = new ArrayList<>();
        for (int i = 0; i < titlesRes.length; i++) {
            titles.add(getResources().getString(titlesRes[i]));
        }

        List<Fragment> fragments = new ArrayList<>();

        fragments.add(OrderUnDeliveredFragment.newInstance("1", search));
        fragments.add(OrderUnPickUpFragment.newInstance("1", search));
        fragments.add(OrderPickedUpFragment.newInstance("1", search));
        fragments.add(OrderTimeOutFragment.newInstance("1", search));

        mAdapter = new BaseFragmentPagerAdapter(getSupportFragmentManager(), fragments, titles);
        viewPager.setAdapter(mAdapter);
        viewPager.setOffscreenPageLimit(fragments.size() - 1);
        tblOrder.setupWithViewPager(viewPager);
        etFragmentSearch.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                new SearchHistoryPopwindow(OrderListActivity.this, search, true, OrderListActivity.this, "orderSearchContent").showAtLocation(viewLine, Gravity.TOP, 0, 0);
            }
        });

        if (null != type && type.equals("overtime")) {
            viewPager.setCurrentItem(3);
        } else if (null != type && type.equals("wait")) {
            viewPager.setCurrentItem(1);
        }
    }

    @Override
    public void onSearch(String searchContent) {
        search = searchContent;
        etFragmentSearch.setText(search);
        mAdapter = null;
        List<String> titles = new ArrayList<>();
        for (int i = 0; i < titlesRes.length; i++) {
            titles.add(getResources().getString(titlesRes[i]));
        }

        List<Fragment> fragments = new ArrayList<>();


        fragments.add(OrderUnDeliveredFragment.newInstance("1", search));
        fragments.add(OrderUnPickUpFragment.newInstance("1", search));
        fragments.add(OrderPickedUpFragment.newInstance("1", search));
        fragments.add(OrderTimeOutFragment.newInstance("1", search));

        mAdapter = new BaseFragmentPagerAdapter(getSupportFragmentManager(), fragments, titles);
        //viewPager = null;
        //viewPager = new ViewPager(RecruitmentActivity.this);
        viewPager.setAdapter(mAdapter);
        viewPager.setOffscreenPageLimit(fragments.size() - 1);
        tblOrder.setupWithViewPager(viewPager);
    }

    @Override
    public void onBack() {
        finish();
    }

    @OnClick(R.id.rl_back)
    public void onViewClicked() {
        finish();
    }
}
